package cn.bb.bbstudy.controller;

import cn.bb.bbstudy.pojo.UserInfo;
import cn.bb.bbstudy.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;

@Controller
public class UserInfoController {

    @Autowired
    UserInfoService userInfoService;
    @Autowired
    private UserService userService;
    @Autowired
    private BlogService blogService;
    @Autowired
    private QuestionService questionService;
    @Autowired
    private CommentService commentService;

    @GetMapping("/userinfo/setting/{uid}")
    public String info(@PathVariable String uid, Model model) {
        callbackInfo(uid, model);
        return "user/setting";
    }


    //根据uid 获取用户信息
    public void callbackInfo(String uid, Model model) {
        //获取用户信息
        UserInfo userInfo = userInfoService.selectInfoByUid(uid);
        model.addAttribute("userInfo", userInfo);
        //如果兴趣栏不为空 也要回显
        String hobby = userInfo.getHobby();
        //不为null 也不为""时
        if (hobby != null && !hobby.isEmpty()) {
            String[] hobbies = hobby.split(",");
            model.addAttribute("hobbies", hobbies);
        }
        //回显 问题数  博客数 回复数
        int qcount = questionService.count(uid);
        int bcount = blogService.count(uid);
        int ccount = commentService.count(uid);
        model.addAttribute("qcount", qcount);
        model.addAttribute("bcount", bcount);
        model.addAttribute("ccount", ccount);
    }

    @PostMapping("/user/update/{uid}")
    public String update(@PathVariable String uid, UserInfo userInfo) {
        userInfoService.updateByUid(userInfo);
        return "redirect:/user/" + uid;
    }
}
